package com.qsd.TeacherSystem.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qsd.TeacherSystem.Service.ClazzService;
import com.qsd.TeacherSystem.Service.CourseService;
import com.qsd.common.po.Clazz;
import com.qsd.common.po.Course;
import com.qsd.common.po.Teacher;
import com.qsd.common.enums.ResultStatus;
import com.qsd.common.vo.BaseVo;
import com.qsd.common.vo.DataVo;

/**
 * @Description 
 * @Author Esion
 * @Data 2020年6月24日
 */
@Slf4j
@RestController
@RequestMapping("course")
public class CourseController {
	
	@Resource
	private CourseService courseService;
	@Resource
	private ClazzService clazzService;
	
	@PostMapping("add")
	public DataVo<Integer> addCourse(Course course, HttpServletRequest request) {
		//获取TeacherUsername
		Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
		Integer add = courseService.add(course, teacher.getUsername());
		if (add != null) {
			return new DataVo<Integer>(ResultStatus.SUCCESS, add);
		}
		return new DataVo<>(ResultStatus.SYSTEM_ERROR);
	}
	
	@GetMapping("removeStudent")
	public BaseVo removeStudentd(String username, Integer id) {
		if (username != null && id != null) {
			return new BaseVo(clazzService.removeStudent(new Clazz(id, username)));
		}
		return new BaseVo(ResultStatus.REQUEST_PARAM_MISS);
	}
	
	@PostMapping("allow")
	public BaseVo allow(Integer id) {
		log.info("课程限制:{}",id);
		return new BaseVo(courseService.allow(id));
	}


	/**
	 * 开始考试
	 * @param courseId
	 * @param paperId
	 * @return
	 */
	@PostMapping("start")
	public BaseVo start(Integer courseId, Integer paperId) {
		return new BaseVo(courseService.start(courseId, paperId));
	}


	/**
	 * 考试结束
	 * @param courseId
	 * @return
	 */
	@PostMapping("stop")
	public BaseVo stop(Integer courseId) {
		return new BaseVo(courseService.stop(courseId));
	}
	
}
